import {getStore, setStore} from '@/util/store'
import {isURL, validatenull} from '@/util/validate'
import {deepClone, encryption} from '@/util/util'
import webiste from '@/const/website'
import {getMenu} from '@/api/admin/menu'

const students = {
  state: {
    studentInfo: getStore({
      name: 'stuInfo'
    }) || '',
    reg_status: '',
    stu_token:  getStore({
      name: 'stu_token'
    }) || '',
    query_UUID: getStore({
      name: 'query_UUID'
    }) || '',
    messageNum: 0
  },
  actions: {
    // 根据用户名登录
    StudentLogin({dispatch,commit}, userInfo) {
      return new Promise((resolve, reject) => {
        studentLogin(userInfo).then(response => {
          commit("SET_Query_UUID", '')
          if(response.code === 10005){
            commit('SET_STUDENT_INFO', response)
            commit('SET_STU_TOKEN', '')
          }else{
            commit('SET_STU_TOKEN', response.data)
            dispatch('GetStuInfo')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetStuInfo({commit}) {
      return new Promise((resolve, reject) => {
        getUserInfo().then((res) => {
          commit('SET_STUDENT_INFO', res)
          resolve()
        }).catch((err) => {
          reject()
        })
      })
    },
    GetMessageNum({commit}) {
      return new Promise((resolve, reject) => {
        getUnreadNum().then((res) => {
          commit('SET_MESSAGE_NUM', res.data)
          resolve()
        }).catch((err) => {
          reject()
        })
      })
    },
  },
  mutations: {
    SET_STU_TOKEN: (state, token) => {
      state.stu_token = token
      setStore({
        name: 'stu_token',
        content: state.stu_token,
        type: 'session'
      })
    },
    SET_Query_UUID: (state, data) => {
      state.query_UUID = data
      setStore({
        name: 'query_UUID',
        content: state.query_UUID,
        type: 'session'
      })
    },
    SHOW_REG: (state, data) => {
      state.reg_status = data
    },
    SET_STUDENT_INFO: (state, userInfo) => {
      state.studentInfo = userInfo
      setStore({
        name: 'stuInfo',
        content: userInfo,
        type: 'session'
      })
    },
    SET_MESSAGE_NUM: (state, num) => {
      state.messageNum = num
    },
    // SET_MENU: (state, params = {}) => {
    //   let {menu, type} = params;
    //   if (type !== false) state.menu = menu
    //   setStore({
    //     name: 'menu',
    //     content: menu,
    //     type: 'session'
    //   })
    // },
    // SET_MENU_ALL: (state, menuAll) => {
    //   state.menuAll = menuAll
    // },
    // SET_ROLES: (state, roles) => {
    //   state.roles = roles
    // },
    // SET_PERMISSIONS: (state, permissions) => {
    //   const list = {}
    //   for (let i = 0; i < permissions.length; i++) {
    //     list[permissions[i]] = true
    //   }
    //   state.permissions = list
    // }
  }

}
export default students
